﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ExpertMIS.Brokers;
using ExpertMIS.Entities;
using System.Collections;

namespace ExpertMIS.UserControl
{
    public partial class ucDomainListWithTopQuestion : System.Web.UI.UserControl
    {
        private DomainBroker _domainBroker = new DomainBroker();
        private QuestionBroker _questionBroker = new QuestionBroker();
        private Hashtable _htImageMapping = new Hashtable();

        private void RefreshDomains()
        {            
            dlDomains.DataSource =  _domainBroker.GetAllDomains();
            dlDomains.DataBind();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            string mappings = ExpertMIS.Codes.Utility.GetEnvironmentValue("domain.images");
            string[] mappingArray = mappings.Split(',');

            foreach (string mapping in mappingArray)
            {
                if (mapping.IndexOf("=") < 0) continue;
                string domainoid = mapping.Substring(0, mapping.IndexOf("=")).Trim();
                string imageurl = mapping.Substring(mapping.IndexOf("=") + 1).Trim();
                if (!_htImageMapping.Contains(domainoid) && !string.IsNullOrEmpty(domainoid))
                    _htImageMapping.Add(domainoid, imageurl);
            }

            RefreshDomains();
        }

        protected void dlDomains_ItemDataBound(object sender, DataListItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                Image imgIndustry = e.Item.FindControl("imgIndustry") as Image;
                ucLastedQuestionList ucQList = e.Item.FindControl("ucLastedQuestionList1") as ucLastedQuestionList;
                ucRecommendExperts ucRExperts = e.Item.FindControl("ucRecommendExperts1") as ucRecommendExperts;
                DomainEntity domain = e.Item.DataItem as DomainEntity;
                HyperLink hlIndustry = e.Item.FindControl("hlIndustry") as HyperLink;

                if (hlIndustry != null && domain != null)
                {
                    hlIndustry.Text = domain.DomainName;
                    hlIndustry.NavigateUrl = "~/Forums/DomainIntroduce.aspx?oid=" + domain.OID;
                }

                if (imgIndustry != null && domain != null && _htImageMapping.Contains(domain.OID))
                {
                    imgIndustry.ImageUrl = _htImageMapping[domain.OID] as string;
                }

                if (ucQList != null && domain != null)
                {
                    ucQList.DomainOID = domain.OID;
                    ucQList.LoadAsDomainQuestion = true;
                    ucQList.CSS = "domain-last-question";
                    ucQList.ShowTitle = false;
                }

                if (ucRExperts != null && domain != null)
                {
                    ucRExperts.DomainOID = domain.OID;
                    ucRExperts.LoadAsDomainExpert = true;
                }
            }
        }
    }
}